home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / music_utilities / pt144.dms / pt144.adf / MIDI_Playground-1.03 / SourceCode / main.c < prev    next >
C/C++ Source or Header  |  1993-01-23  |  5KB  |  194 lines

  1. /**************************************************************************
  2. * main.c:    The main program and supporting stuff.
  3. *        Part of MP, the MIDI Playground.
  4. *
  5. * Author:    Daniel Barrett
  6. * Version:    See the file "version.h".
  7. * Copyright:    None!  This program is in the Public Domain.
  8. *        Please share it with others.
  9. ***************************************************************************/
  10.  
  11.     
  12. #include "mp.h"
  13. #include "version.h"
  14.  
  15. BOOL WorkbenchProgram(char *argv[]);
  16. BOOL CommandLineProgram(int argc, char *argv[]);
  17.  
  18. char *version = "$VER: MP/MIDI_Playground "  VERSION " " VERSION_DATE;
  19.     
  20. main(int argc, char *argv[])
  21. {
  22.     BOOL success;
  23.  
  24.     if (argc == 0)
  25.         success = WorkbenchProgram(argv);
  26.     else
  27.         success = CommandLineProgram(argc, argv);
  28.  
  29.     exit(success ? RETURN_OK : RETURN_FAIL);
  30. }
  31.  
  32.     
  33. /************************************************************************
  34. * Initialization of important variables.
  35. ************************************************************************/
  36.  
  37. void InitStuff(FLAGS theFlags[], FILE **in, FILE **out, char **inFile,
  38.         char **outFile)
  39. {
  40.     int i;
  41.  
  42.     for (i=0; i<NUM_FLAGS; i++)    /* Flags. */
  43.         theFlags[i] = (FLAGS)0;
  44.  
  45.     *in      = stdin;        /* Input file. */
  46.     *out     = stdout;        /* Output file. */
  47.     *inFile  = NULL;        /* Input file name. */
  48.     *outFile = NULL;        /* Output file name. */
  49. }
  50.  
  51.     
  52. /************************************************************************
  53. * Open the serial port if necessary, and have fun!
  54. ************************************************************************/
  55.  
  56. BOOL MIDIPlayground(FLAGS theFlags[], FILE *in, FILE *out)
  57. {
  58.     if ((theFlags[FLAG_ITYPE] == OPT_MIDI)
  59.     ||  (theFlags[FLAG_OTYPE] == OPT_MIDI))
  60.     {
  61.         if (!SerialSetup(theFlags[FLAG_SYSEX]))
  62.             return(FALSE);
  63.         ResetSerialPort();
  64.     }
  65.  
  66.     ReadAndWriteStuff(theFlags, in, out);
  67.  
  68.  
  69.     if ((theFlags[FLAG_ITYPE] == OPT_MIDI)
  70.     ||  (theFlags[FLAG_OTYPE] == OPT_MIDI))
  71.         SerialShutdown();
  72.  
  73.     return(TRUE);        /* Stub. */
  74. }
  75.  
  76.     
  77. /************************************************************************
  78. * Use the appropriate I/O functions until we're done.
  79. ************************************************************************/
  80.  
  81. void ReadAndWriteStuff(FLAGS theFlags[], FILE *in, FILE *out)
  82. {
  83.     MIDI_RESULT    result;
  84.     MIDI_VALUE    value;
  85.     MIDI_RESULT    (*getfcn)(FILE *f, MIDI_VALUE *value);
  86.     BOOL        (*putfcn)(FILE *f, MIDI_VALUE value);
  87.     void        (*skipfcn)(FILE *f, MIDI_VALUE value);
  88.  
  89.     SetTheFunctions(&getfcn, &putfcn, &skipfcn, theFlags);
  90.     
  91.     while ((result = getfcn(in, &value)) != RESULT_STOP)
  92.     {
  93.         switch (result)
  94.         {
  95.             case RESULT_OK:
  96.                 putfcn(out, value);
  97.                 break;
  98.             case RESULT_ERROR:
  99.                 fprintf(out, "ERROR\n");
  100.                 skipfcn(in, value);
  101.                 break;
  102.             case RESULT_OVERFLOW:
  103.                 fprintf(out, "VALUE TOO BIG (overflow)\n");
  104.                 skipfcn(in, value);
  105.                 break;
  106.             default:
  107.                 skipfcn(in, value);
  108.                 fprintf(out, "An unknown error occurred.\n");
  109.                 break;
  110.         }
  111.     }
  112. }
  113.  
  114.  
  115. /************************************************************************
  116. * Choose the appropriate I/O functions.
  117. ************************************************************************/
  118.  
  119. void SetTheFunctions(MIDI_RESULT (**getfcn)(), BOOL (**putfcn)(),
  120.             void (**skipfcn)(), FLAGS theFlags[])
  121. {
  122.     switch (theFlags[FLAG_ITYPE])
  123.     {
  124.         case OPT_TEXT:
  125.             *getfcn = FromText;
  126.             *skipfcn = SkipText;
  127.             break;
  128.         case OPT_BINARY:
  129.             *getfcn = FromBinary;
  130.             *skipfcn = SkipBinary;
  131.             break;
  132.         case OPT_MIDI:
  133.             *getfcn = FromMidi;
  134.             *skipfcn = SkipMidi;
  135.             break;
  136.         default:
  137.             *getfcn = FromText;    /* Least dangerous fcn. */
  138.             *skipfcn = SkipText;    /* Least dangerous fcn. */
  139.     }
  140.  
  141.     switch (theFlags[FLAG_OTYPE])
  142.     {
  143.         case OPT_TEXT:
  144.             *putfcn = ToText;
  145.             break;
  146.         case OPT_BINARY:
  147.             *putfcn = ToBinary;
  148.             break;
  149.         case OPT_MIDI:
  150.             *putfcn = ToMidi;
  151.             break;
  152.         default:
  153.             *putfcn = ToText;    /* Least dangerous fcn. */
  154.             break;
  155.     }
  156. }
  157.  
  158.  
  159. /* Is "opt" a valid input/output type?  Set the flag too. */
  160.     
  161. BOOL CheckIOType(char opt, int ioFlag, FLAGS theFlags[])
  162. {
  163.     return(IsIOType(theFlags[ioFlag] = opt));
  164. }
  165.  
  166.  
  167. /* Is "ioType" a valid input/output type? */
  168.     
  169. BOOL IsIOType(char ioType)
  170. {
  171.     return(    (ioType == OPT_TEXT)
  172.     ||    (ioType == OPT_BINARY)
  173.     ||    (ioType == OPT_MIDI));
  174. }
  175.  
  176.  
  177. /* Did user specify both input and output types? */
  178.     
  179. BOOL CheckFlags(FLAGS theFlags[])
  180. {
  181.     return(theFlags[FLAG_ITYPE] && theFlags[FLAG_OTYPE]);
  182. }
  183.  
  184.     
  185. /************************************************************************
  186. * ^C handling -- Manx stuff.
  187. ************************************************************************/
  188.  
  189. void _abort(void)
  190. {
  191.     SerialShutdown();
  192.     exit(RETURN_FAIL);
  193. }
  194.